package com.patri.tema1;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collection;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Main extends Activity{

private Button adaugaOrasButton;
public lista1 adaptorOrase= new lista1(this);
private ListView listView;
private final static String FILENAME = "orase.txt";
String global="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    adaugaOrasButton = (Button) findViewById(R.id.adaugaOrasButton);
    adaugaOrasButton.setKeepScreenOn(true);
    //adaptorOrase.adaugaOrasRandom();
    
    //Collection.sort(orase);
    adaugaOrasButton.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
			// Adaugam un personaj nou in lista
			//adaptorOrase.adaugaOrasRandom();
			Intent intent = new Intent();
			intent.setClass(Main.this, myDialog.class);
			startActivityForResult(intent,100);
			
			//Intent intent2 = getIntent();
		    //nume = intent.getStringExtra("tara");
		    Log.d ("patri", "tara: "+global);
		    //if (nume!=null) adaptorOrase.adaugaOrasRandom();
		}
	});
    
    //preluam referinta catre lista grafica
    listView = (ListView)findViewById(R.id.listaorase);
    
    //setam adaptorul (controllerul) listei
    listView.setAdapter(adaptorOrase);

    //setam actiune pe apasare lunga
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
		public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
			//stergem personajul selectat
			adaptorOrase.stergeOras(pos);
			return true;
			
			 
		}
	});
    
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
			
			Intent intent = new Intent();
			intent.setClass(Main.this,Testxml4.class);
			intent.putExtra("numeoras", adaptorOrase.orase.get(pos).getNumeOras().toLowerCase());
			startActivity(intent);			
			 
		}
	}); 
    
}

@Override
public void onActivityResult (int requestCode, int responseCode, Intent data)
{
	// raspunsul vine de la formularul de adaugare
	// acolo am cerut raspunsul cu nr 100
	if (requestCode == 100)
	{
		// s-a apasat butonul Adauga
		if (responseCode==1)
		{
			global = data.getStringExtra("oras");
			
			Log.d ("patri", "oras: "+global);
			if (global!=""&& global!=" \n" )
			adaptorOrase.adaugaOras(global);
			
			// adaptor.adaugaFilm (nume, an, regizor);
		}
		// s-a apasat butonul Renunta
		else if (responseCode==0)
		{
		}
	}
}
@Override
public void onResume() {
	super.onResume();

	try {
    	InputStream in = openFileInput(FILENAME);
    	if (in != null) {
    		InputStreamReader tmp = new InputStreamReader(in);
	    	BufferedReader reader = new BufferedReader(tmp);
	    	String str="";
	    	StringBuffer buf = new StringBuffer();
	    	while ((str = reader.readLine()) != null) {
	    		
	    		Log.d ("patri", "str in fisier:" + str+"aaa");
	    		if((str!="") && (str!="\n") )
	    		adaptorOrase.adaugaOras(str);
	    		buf.append(str);
	    	}
	    	in.close();
	    	
    	}
	}catch(java.io.FileNotFoundException e) {
		// that's OK, we probably haven't created it yet
	} catch (Throwable t) {
	Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
	}
    	}
@Override
public void onPause() {
	super.onPause();
	try {
    	OutputStreamWriter out = new OutputStreamWriter(openFileOutput(FILENAME, MODE_APPEND));
    	if(global!="") out.write(global+"\n" );
    	
    	out.close();
	} catch (Throwable t) {
	Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
	}
}
}